package com.hsnn.medstgmini.util;

import java.awt.Color;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map.Entry;

import com.lowagie.text.Cell;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Font;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Table;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfWriter;

/**
 * 
 * @category pdf公用类
 * @author 应晓川
 * @date 2015年9月6日11:14:48
 */
public class PdfUtil {
	public static String rootPath="";
	/**
	 * 
	 * @category 根据sql结果集，生成pdf文件
	 * @author 应晓川
	 * @date 2015年9月6日11:15:39
	 * @param list
	 * @return String file 保存路径地址
	 * @throws Exception
	 */
	public static String execSQLtoPdf(List<HashMap<String, Object>> list,String scriptName,String rootPath,String filePath)throws Exception {
		Document document = new Document(PageSize.A4.rotate()); // A4纸
		PdfUtil.rootPath = rootPath;
		rootPath = rootPath.replaceAll("\\\\", "/") + filePath;
		String fileTemp = "pub"+ System.currentTimeMillis()+".pdf";
		String FPFfile = "/"+filePath+fileTemp;
		String file =  rootPath+fileTemp;
		PdfWriter.getInstance(document, new FileOutputStream(file));
		/* 开启 */
		document.open();
		Table table = new Table(1);
		table.setBorderWidth(0);
		table.setPadding(3);// 内部高度
		table.setAlignment(1);// 表单居中
		table.setSpacing(0);// 内部表格边间距
		Cell cell = null;
		if (list.size() > 0) {
			HashMap<String, Object> map = list.get(0);
			table = new Table(list.get(0).size() + 1);// 表格布局

			table.setBorderWidth(0);
			table.setPadding(3);// 内部高度
			table.setAlignment(1);// 表单居中
			table.setSpacing(0);// 内部表格边间距

			cell = new Cell(new Paragraph(scriptName, setChineseFont(0)));// 设置表格标题
			cell.setHeader(true);
			cell.setColspan(list.get(0).size() + 1);
			cell.setBackgroundColor(new Color(255, 255, 255));
			table.addCell(cell);
			table.endHeaders();

			List<String> listTemp = new ArrayList<String>();

			table.addCell(new Paragraph("序号", setChineseFont(0)));
			for (Entry<String, Object> entry : map.entrySet()) {//获取列名放入list集合
				table.addCell(new Paragraph(entry.getKey(), setChineseFont(0)));
				listTemp.add(entry.getKey());
			}

			int i = 0;
			for (HashMap<String, Object> f : list) {// 赋值
				table.addCell(new Paragraph(i + 1 + ""));
				String key = "";
				for (int j = 0; j < listTemp.size(); j++) {
					key = listTemp.get(j);
					if(key.equals("价格") || key.equals("限价") ){//红色字体
						table.addCell(new Paragraph(String.valueOf(f.get(key)), setChineseFont(1)));	
					}else{
						table.addCell(new Paragraph(String.valueOf(f.get(key)), setChineseFont(0)));	
					}
					
				}
				i++;
			}
		} else {
			cell = new Cell(new Paragraph("查询数据无！", setChineseFont(0)));
			cell.setColspan(1);
			table.addCell(cell);
			return null;
		}
		// 下面三行表示每一个列表的列名

		/* 关闭 */
		document.add(table);
		document.close();
		return FPFfile;
	}
	// 中文转码
	public static Font setChineseFont(int tags) {
		BaseFont bf = null;
		Font fontChinese = null;
		try {
			//String path = PdfUtil.class.getClassLoader().getResource("").getPath();
			String path = PdfUtil.rootPath;
			path = path.replaceAll("\\\\", "/")+"WEB-INF/font/SIMKAI.TTF";
			bf = BaseFont.createFont(path,BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
			  if(tags==0){
            	  fontChinese = new Font(bf, 10, Font.NORMAL);//默认字体大小
            }else if(tags==1){
            	fontChinese = new Font(bf, 15, Font.NORMAL,new Color(255,0,0));//默认字体大小
            }
		} catch (DocumentException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return fontChinese;
	}
}